/*
 * @Description:
 * @Author: Chenx
 * @Date: 2023-04-11 15:48:19
 * @LastEditors: Chenx
 * @LastEditTime: 2023-04-19 16:38:22
 */
const path = require('path')
const { defineConfig } = require('@vue/cli-service')
// 组件自动加载
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')

const { VUE_APP_BASE_URL } = process.env

module.exports = defineConfig({
  transpileDependencies: true,
  // 自动按需加载
  configureWebpack: {
    plugins: [
      AutoImport({
        dirs: ['src/utils'],
        dts: 'src/auto-imports.d.ts',
        imports: ['vue', 'vue-router'],
        eslintrc: {
          enabled: true, // 默认false, true启用。生成一次就可以，避免每次工程启动都生成
          filepath: './.eslintrc-auto-import.json', // 生成json文件,eslintrc中引入
          globalsPropValue: true
        }
      }),
      Components({
        resolvers: [ElementPlusResolver()],
        dirs: ['src/components', 'src/layout'],
        dts: 'src/components.d.ts'
        // 允许子目录作为组件的命名空间前缀。
        // directoryAsNamespace: true
      })
    ],
    resolve: {
      extensions: ['.js', '.vue', '.json', '.ts'],
      alias: {
        '@': path.resolve(__dirname, 'src/')
      }
    }
  },
  css: {
    // 开启 CSS source maps?
    sourceMap: false,
    loaderOptions: {
      less: {
        charset: false
      },
      scss: {
        additionalData: `
        @use "./src/assets/style/element-variables.scss" as *;
        @import "./src/assets/style/_mixins.scss";
        @import "./src/assets/style/global.scss";
        `
      }
    },
    extract: {
      ignoreOrder: true
    }
  },
  devServer: {
    host: '0.0.0.0',
    port: 8080,
    // open: true, // 自动打开浏览器
    proxy: {
      '/api': {
        target: VUE_APP_BASE_URL, // 目标代理接口地址
        secure: false,
        changeOrigin: true, // 开启代理，在本地创建一个虚拟服务端
        // ws: true, // 是否启用websockets
        onProxyReq (proxyReq, req, res) {
          console.log('[HPM] %s %s %s %s', req.method, req.originalUrl, '->', VUE_APP_BASE_URL)
          console.log('[HPM] Rewriting path from "%s" to "%s"', req.originalUrl, req.url)
        },
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  }
})
